# from flask import request, jsonify
import json
from werkzeug.exceptions import HTTPException

__author__ = 'jenrey'


class APIException(HTTPException):
    code = 500
    error_code = 5001
    msg = 'Sorry, there was an unexpected error(*^v^*)'
    data= None

    def __init__(self, msg=None, code=None, error_code=None,data=None):

        if error_code:
            self.error_code = error_code
        if code:
            self.code = code
        if msg:
            self.msg = msg
        if data:
            self.data = data
        super().__init__(msg, None)

    def get_body(self,environ=None,scope=None):
        body = dict(
            code=self.code,
            error_code=self.error_code,
            msg=self.msg
        )
        if self.data :
            body['data']=self.data

        text = json.dumps(body)

        return text

    def get_headers(self,environ=None,scope=None):
        return [("Content-Type", "application/json")]

